<?php
namespace BaseExt;
class Pic {
    function ResizeImage($fromPath,$toPath,$maxWidth = 2000,$maxHeight = 2000)
    {
        $fromPath = ".".$fromPath;
        $toPath = ".".$toPath;
        $image = imagecreatefromjpeg($fromPath);
        //取得当前图片大小
        $width = imagesx($image);
        $height = imagesy($image);
        //生成缩略图的大小
        if(($width > $maxWidth) || ($height > $maxHeight))
        {
            $widthRatio = $maxWidth/$width;
            $heightRatio = $maxHeight/$height;

            if($widthRatio < $heightRatio)
            {
             $ratio = $widthRatio;
            }
            else
            {
              $ratio = $heightRatio;
            }

            $newWidth = $width * $ratio;
            $newHeight = $height * $ratio;
//            $i=0.5;
//            $newWidth = $width * $i;
//            $newHeight = $height * $i;
            if(function_exists("imagecopyresampled"))
            {
                $image_resize = imagecreatetruecolor($newWidth, $newHeight);
                imagecopyresampled($image_resize, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
            }
            else
            {
                $image_resize = imagecreate($newWidth, $newHeight);
                imagecopyresized($image_resize, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
            }

            ImageJpeg ($image_resize,$toPath);
            ImageDestroy ($image_resize);
        }
        else
        {
            ImageJpeg ($image,$toPath);
        }
    }




}
